<?php

class PerfilDao extends Dao {

    public function injectDependencies() {

    }

    public function findAll() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindAllProfiles();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArProfiles = array();
        while($ArProfile = mysqli_fetch_assoc($tQuery)) {
            $Profile = new Perfil($ArProfile['IDPerfil'], $ArProfile['IDFuncao'], $ArProfile['StPerfil'], $this->tMysqli);
            $Profile->loadPermissions();
            $ArProfiles[] = $Profile;
        }
        $this->closeConnection();
        return $ArProfiles;
    }

    public function save($ArDados) {
        $this->initTransaction();
        $this->tMysqli->escape($ArDados);
        $StSql = TQuery::getSqlInsertProfile($ArDados);
        $this->tMysqli->exec($StSql);
        $IDPerfil = $this->tMysqli->lastInsertId();
        foreach($ArDados['ArActions'] as $IDAction) {
            $StSqlInsertActionToProfile = TQuery::getSqlInsertActionToProfile($IDAction, $IDPerfil);
            $this->tMysqli->exec($StSqlInsertActionToProfile);
        }
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();;
    }

    public function findById($IDPerfil) {
        $this->initConnection();
        $this->tMysqli->escape($IDPerfil);
        $StSql = TQuery::getSqlFindProfileById($IDPerfil);
        $tQuery = $this->tMysqli->exec($StSql);
        while($ArPerfil = mysqli_fetch_assoc($tQuery)) {
            $Profile = new Perfil($ArPerfil['IDPerfil'], $ArPerfil['IDFuncao'], $ArPerfil['StPerfil'], $this->tMysqli);
            $Profile->loadPermissions();
        }
        $this->closeConnection();
        return isset($Profile) ? $Profile : false;
    }

    public function update($ArDados) {
        $this->initConnection();
        $StSql = TQuery::getSqlUpdateProfile($ArDados);
        $this->tMysqli->exec($StSql);
        $BoReturn = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoReturn;
    }

    public function findByPosition($IDFuncao) {
        $this->initConnection();
        $this->tMysqli->escape($IDFuncao);
        $StSql = TQuery::getSqlFindProfileByPosition($IDFuncao);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArProfiles = array();
        while($ArProfileData = mysqli_fetch_assoc($tQuery)) {
            $ArProfiles[] = new Perfil($ArProfileData['IDPerfil'], $ArProfileData['IDFuncao'], $ArProfileData['StPerfil'], $this->tMysqli);
        }
        $this->closeConnection();
        return $ArProfiles;
    }

    public function changePosition($ArPositionsId) {
        $this->initTransaction();
        $this->tMysqli->escape($ArPositionsId);
        foreach( $ArPositionsId as $IDPerfil => $IDFuncao) {
            $StSql = TQuery::getSqlUpdateChangePositionProfileById($IDFuncao, $IDPerfil);
            $this->tMysqli->exec($StSql);
        }
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function delete($IDPerfil) {
        $this->initConnection();
        $this->tMysqli->escape($IDPerfil);
        $StSqlDeletePermissions = TQuery::getSqlDeletePermissionsByProfile($IDPerfil);
        $StSqlDeleteProfile = TQuery::getSqlDeleteProfile($IDPerfil);
        $this->tMysqli->exec($StSqlDeletePermissions);
        $this->tMysqli->exec($StSqlDeleteProfile);
        $BoRetorno = $this->tMysqli->lastQueryReturn();
        $this->closeConnection();
        return $BoRetorno;
    }

}

?>
